package com.wellink.baseadapterdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

public class MyBaseAdapater extends BaseAdapter {

    private Context  context;
    private List<Data> dataList;
    private int resId;

    public MyBaseAdapater(Context context, List<Data> dataList, int resId) {
        this.context = context;
        this.dataList = dataList;
        this.resId = resId;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);

        View view = inflater.inflate(resId,null);

        ImageView imageView = view.findViewById(R.id.icon);
        TextView tv1 = view.findViewById(R.id.title);
        TextView tv2 = view.findViewById(R.id.info);

        Data data = dataList.get(position);
        if(null != data){
            imageView.setImageResource(data.getIcon());
            tv1.setText(data.getTitle());
            tv2.setText(data.getInfo());
        }

        return view;
    }
}
